package sim;

import java.io.*;

/**
 * The logger.
 */
public class Logger {
	static String log = "log_";
	static String s;
	static PrintWriter out = null;
	
	static BufferedWriter outputBuffer = null;
	static PrintWriter printstream = null;


	public static void log(Object o) {
		if (out != null)
			out.println(Sim.steps + ": " + o);
	}

	public static void init() {
		int i = 0;
		File file = new File(log + i);
		while (file.exists())
			file = new File(log + ++i);

		try {
			out = new PrintWriter(file);
			
			//added 
			outputBuffer = new BufferedWriter(out);
			printstream = new PrintWriter(outputBuffer);
			
		} catch (FileNotFoundException e) {
			out = null;
			Sim.err("Logger error; logging disabled.");
		}
	}

	public static void end() {
//		if (out != null) {
//			out.flush();
//			out.close();
//		}
		
		if (printstream != null) {
			printstream.flush();
			printstream.close();
		}
		
	}

	public static void logAndPrint() {
//		if (out != null)
//			out.println(s);
//		Sim.print(s);
		
		if (printstream != null)
			printstream.println(s);
		Sim.print(s);
	}

	public static void logBeginSim() {
		s = "Simulation begins cost2.";
		logAndPrint();
	}

	public static void logEndSim() {
		s = "Simulation ends.";
		logAndPrint();
	}

	public static void logRunIncomplete(int run, int runs) {
		s = "Run " + run + " of " + runs + " exceeded time limit.";
		logAndPrint();
	}

	public static void logRunComplete(int run, int runs, long steps) {
		s = "Run " + run + " of " + runs + " completed in " + steps + " steps.";
		logAndPrint();
	}

	public static void logExit() {
		s = "Exited by user.";
		logAndPrint();
	}
	
	
}
